home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / ARCADE.ZIP / MS.EXE / lzh / ARC10_1.CHP < prev    next >
Encoding:
Text File  |  1993-06-15  |  7.6 KB  |  224 lines

  1. %
  2. #EF
  3. #T15,1,Chapter 10     Some Final Thoughts     Pg. 1
  4. #HS,1,4,80,25,11,1
  5. #C4,R5
  6.                             ~W~IMaking The Jump To C++~Y~I
  7.  
  8. Throughout this book, we've tried to use ideas that are normally only
  9. found in object oriented languages such as C++. We've been creating
  10. objects using C's structures, and hiding the implementation of the objects
  11. with macro interfaces. This is very much like what the class does for
  12. programmers in C++. As you can see, we don't have to limit object oriented
  13. techniques to object oriented languages.
  14.  
  15. #WN
  16. Using structures with macro interfaces has made the programs we've
  17. developed a bit wordy. However, I've found that this style of programming
  18. helps make the transition to C++ much easier for the students in my C
  19. courses. More and more, new development is being done in object oriented
  20. languages such as C++.
  21.  
  22. #WN
  23. %
  24. #EF
  25. #T15,1,Chapter 10     Some Final Thoughts     Pg. 2
  26. #HS,1,4,80,25,11,1
  27. #C4,R5
  28.                                ~W~ITrue Objects~Y~I
  29.  
  30. The objects, such as attackers or polygons, that we've defined for our
  31. games aren't objects in the truest sense of the objects found in object
  32. oriented languages. They lack a number of features that are present in C++
  33. classes. To be specific, the C objects that we've used have no ~R~Iprivate
  34. data, no member functions, no inheritance, and no polymorphism~Y~I.
  35.  
  36.  
  37.                                  ~W~IPrivate Data~Y~I
  38.  
  39. Objects in C++ are created by creating classes. An example of a simple
  40. class, a point, is shown in Figure 10.1 on the next page. A C++ class has
  41. members just like a C structure. However, unlike a structure, the section
  42. that contains the data in a class is usually private. Being private, the
  43. data may only be accessed by member functions (which are discussed below).
  44. Because of this, access to an object's data is very controlled.
  45. #WP
  46. %
  47. #EF
  48. #T15,1,Chapter 10     Some Final Thoughts     Pg. 3
  49. #HS,1,4,80,25,11,1
  50. #C4,R5
  51.                                    ~W~IFigure 13.1~Y~I
  52.                                A Simple C++ Class
  53.  
  54.     class point
  55.     {
  56.     private:
  57.         int x,y;
  58.     public:
  59.         void set_row(int row) {y=row;}
  60.         void set_col(int col) (x=col;}
  61.         int get_row(void) {return y;}
  62.         int get_col(void) {return x;}
  63.     };
  64.  
  65.  
  66. #WP
  67. %
  68. #EF
  69. #T15,1,Chapter 10     Some Final Thoughts     Pg. 4
  70. #HS,1,4,80,25,11,1
  71. #C4,R5
  72. ~Y~IThe great advantage of a C++ class over a C structure is that a C++ class
  73. forces programmers to use the member functions to access data. The C
  74. structures we implemented had macro interfaces, but there's no way to
  75. force the maintenance programmer to use the macros. He/she could easily
  76. bypass our wonderful interfaces and completely destroy the re-usability of
  77. our carefully crafted code.
  78.  
  79. #WN
  80. Having the data private helps hide the implementation of the class. The
  81. implementation can be changed quite radically without affecting the rest
  82. of the program as long as the interface to the class, which is formed by
  83. the member functions, stays the same.
  84.  
  85. #WP
  86. %
  87. #EF
  88. #T15,1,Chapter 10     Some Final Thoughts     Pg. 5
  89. #HS,1,4,80,25,11,1
  90. #C4,R5
  91.                               ~W~IMember Functions~Y~I
  92.  
  93. As the example above shows, C++ classes not only have member data, but
  94. member functions as well. These functions are usually part of the public
  95. section of a class. They are used to provide the set of all valid
  96. operations that may be done on an object. Each type of object has its own
  97. member functions.
  98.  
  99. #WN
  100. Encapsulating member functions with member data makes C++ objects more of
  101. a single organic unit. That helps to make code more portable and re-
  102. usable. It also helps to isolate errors and simplify the debugging
  103. process, which is usually not a fun task.
  104.  
  105. #WP
  106. %
  107. #EF
  108. #T15,1,Chapter 10     Some Final Thoughts     Pg. 6
  109. #HS,1,4,80,25,11,1
  110. #C4,R5
  111.                                ~W~Inheritance~Y~I
  112.  
  113. Our objects have acquired the characteristics of other objects by including
  114. a member in the structure of the other object. For instance, a space
  115. attacker acquired the attributes of an object of type character by having a
  116. member of type character in the attacker. This is not really inheritance,
  117. it's actually called containership. But it's as close as C can get to
  118. inheritance.
  119.  
  120. #WN
  121. True inheritance means that the inheriting object is also partially
  122. composed of the inherited object. An example is given in Figure 10.2.
  123. #WP
  124. %
  125. #EF
  126. #T15,1,Chapter 10     Some Final Thoughts     Pg. 7
  127. #HS,1,4,80,25,11,1
  128. #C4,R5
  129.                                 ~W~IFigure 10.2~Y~I
  130.                                Inheritance
  131.  
  132.     class point
  133.     {
  134.     private:
  135.         int x,y;
  136.     public:
  137.         void set_row(int row) {y=row;}
  138.         void set_col(int col) (x=col;}
  139.         int get_row(void) {return y;}
  140.         int get_col(void) {return x;}
  141.     }
  142. #C1,R23
  143.                              ~C~IContinued On Next Page~Y~I
  144.  
  145. #WP
  146. %
  147. #EF
  148. #T15,1,Chapter 10     Some Final Thoughts     Pg. 8
  149. #HS,1,4,80,25,11,1
  150. #C4,R5
  151.                             ~W~IFigure 10.2 (cont)~Y~I
  152.                                Inheritance
  153.  
  154.     class pixel : public point
  155.     {
  156.     private:
  157.         int color;
  158.     public:
  159.         get_color(void) {return color;}
  160.         void set_color(int new_color) {color=new_color;}
  161.     }
  162.  
  163. #WP
  164. %
  165. #EF
  166. #T15,1,Chapter 10     Some Final Thoughts     Pg. 9
  167. #HS,1,4,80,25,11,1
  168. #C4,R5
  169. ~Y~I
  170. In this example we've created a new object called a pixel. A pixel itself
  171. has only one piece of member data, the color. However, the statement
  172. public point, on the first line of the pixel class definition means that a
  173. pixel inherits all of the functionality of a point. All of the
  174. functionality means just that. It inherits both member data and member
  175. functions. That means that code can be reused very easily. We can quickly
  176. build very complex objects from much simpler ones without having to
  177. rewrite code.
  178.  
  179. #WP
  180. %
  181. #EF
  182. #T15,1,Chapter 10     Some Final Thoughts     Pg. 10
  183. #HS,1,4,80,25,11,1
  184. #C4,R5
  185.                                ~W~IPolymorphism~Y~I
  186.  
  187. Polymorphism is a four-bit word for a two-bit idea. What it means is that,
  188. in object oriented languages like C++, the programmer has the ability to
  189. define many functions with the same name. The advantage of this is not
  190. immediately obvious until you look at a few examples.
  191.  
  192. #WN
  193. C doesn't support strings, at least not to the extent that languages such
  194. as Basic and Pascal do. In Pascal, you can make the statement
  195. str1=str2+str3, where str1, str2, and str3 are strings. The result of the
  196. statement is that the concatenation of str2 and str3 is stored in str1. It
  197. would be nice to be able to add this same ability to C's strings.
  198.  
  199. #WP
  200. %
  201. #EF
  202. #T15,1,Chapter 10     Some Final Thoughts     Pg. 11
  203. #HS,1,4,80,25,11,1
  204. #C4,R5
  205. ~Y~I
  206. In C++, this is easy to do. Since we can have many functions with the same
  207. name, polymorphism, we can write a new function called + that works for
  208. strings. If we wanted to, we could also define a + function for points,
  209. polygons, and segments. We can have as many versions of + as we want
  210. to. The C++ compiler knows what type of objects we're adding together, so
  211. it calls the appropriate + function for that type of object.
  212.  
  213. #WN
  214.                           ~W~IThe Long And Short Of Objects~Y~I
  215.  
  216. As I've stated, I developed the style of programming that I've presented
  217. to help make the transition to C++ easier. I hope that you will seriously
  218. consider making that transition as soon as possible. The advanced language
  219. features you acquire when you move to object oriented languages will enable
  220. you to build very complex games, and other types of programs, with much
  221. greater ease than is otherwise possible.
  222. #WP
  223. #X
  224.